package org.game.db.dao;

import com.sojoys.artifact.manager.DataSourceManager;
import java.sql.Connection;
import com.sojoys.artifact.build.data.base.BaseDao;
import java.util.ArrayList;
import org.game.db.bean.Mail;
import com.sojoys.artifact.manager.JsonManager;
import static org.game.db.StaticResource.MailResource.*;
import java.sql.SQLException;
import com.google.common.collect.Lists;
import org.apache.commons.dbutils.handlers.MapHandler;
import com.sojoys.artifact.tools.SK_Plus;
import java.sql.ResultSet;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.handlers.MapListHandler;
import com.sojoys.artifact.constant.CreateTableEnum;
import com.sojoys.artifact.constant.DataModelKeyEnum;
import org.apache.commons.dbutils.handlers.AbstractListHandler;
import org.apache.commons.dbutils.QueryRunner;
import com.xiaoleilu.hutool.log.Log;
import java.util.Set;
import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.xiaoleilu.hutool.util.StrUtil;
import com.sojoys.artifact.tools.ToolError;
import org.apache.commons.dbutils.DbUtils;
import com.xiaoleilu.hutool.log.LogFactory;
import java.util.List;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.dbutils.ResultSetHandler;
import com.google.common.base.Joiner;
import static org.game.db.StaticResource.MailResource.*;
/**
 * 邮件
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MailDaoImpl implements BaseDao<Long,Mail>{
	static Log log = LogFactory.get(MailDaoImpl.class);
	
	// 代理
	
	/**
	 * 私有化无参构造方法
	 */
	private MailDaoImpl() {}
	  
    private static final MailDaoImpl INSTANCE = new MailDaoImpl();  
    
    public static MailDaoImpl me() {  
        return INSTANCE;  
    }  
	
	public Mail insert(Mail mail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(mail,conn);
	}
	
	public Mail insert(Mail mail,Connection conn){
		return insert(mail,conn,TABLE_NAME);
	}
	
	public Mail insert(Mail mail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insert(mail,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Mail insert(Mail mail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insert(mail,conn,tableName);
	}
	
	public Mail insert(Mail mail,Connection conn,String tableName){
		
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			int i = run.update(conn,sql,mail.getId(),mail.getSenderId(),mail.getPlayerId(),mail.getTextType(),mail.getTitle(),mail.getBody(),mail.getBodyParamString(),mail.getItemsByte(),mail.getStatus(),mail.getDsp(),mail.getCreateDate(),mail.getModifiedDate());
			return i > 0 ? mail : null;
		} catch (Exception e) {
			log.error(e);
			return null;
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public Mail insert(Mail mail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insert(mail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public int[] insertBatch(List<Mail> mails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(mails,conn);
	}
	
	public int[] insertBatch(List<Mail> mails,Connection conn){
		return insertBatch(mails,conn,TABLE_NAME);
	}
	
	public int[] insertBatch(List<Mail> mails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(mails,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] insertBatch(List<Mail> mails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return insertBatch(mails,conn,tableName);
	}
	
	public int[] insertBatch(List<Mail> mails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "INSERT INTO " +tableName+ " (id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			int columnSize = 12;
			int size = mails.size();
			Object[][] params = new Object[size][columnSize];
			for (int i = 0; i < size; i++) {
				params[i][0] =mails.get(i).getId();
				params[i][1] =mails.get(i).getSenderId();
				params[i][2] =mails.get(i).getPlayerId();
				params[i][3] =mails.get(i).getTextType();
				params[i][4] =mails.get(i).getTitle();
				params[i][5] =mails.get(i).getBody();
				params[i][6] =mails.get(i).getBodyParamString();
				params[i][7] =mails.get(i).getItemsByte();
				params[i][8] =mails.get(i).getStatus();
				params[i][9] =mails.get(i).getDsp();
				params[i][10] =mails.get(i).getCreateDate();
				params[i][11] =mails.get(i).getModifiedDate();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] insertBatch(List<Mail> mails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return insertBatch(mails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public Mail update(Mail mail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(mail,conn);
	}
	
	public Mail update(Mail mail,Connection conn){
		return update(mail,conn,TABLE_NAME);
	}
	
	public Mail update(Mail mail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return update(mail,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Mail update(Mail mail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return update(mail,conn,tableName);
	}
	
	public Mail update(Mail mail,Connection conn,String tableName){
		List<Mail> mails = new ArrayList<Mail>();
		mails.add(mail);
		updateBatch(mails,conn,tableName);
		return mail;
	}
	
	public Mail update(Mail mail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return update(mail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<Mail> mails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(mails,conn);
	}
	
	public int[] updateBatch(List<Mail> mails,Connection conn){
		return updateBatch(mails,conn,TABLE_NAME);
	}
	
	public int[] updateBatch(List<Mail> mails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(mails,conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public int[] updateBatch(List<Mail> mails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return updateBatch(mails,conn,tableName);
	}
	
	public int[] updateBatch(List<Mail> mails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "UPDATE " +tableName+ " SET id = ?,sender_id = ?,player_id = ?,text_type = ?,title = ?,body = ?,body_param = ?,items = ?,status = ?,dsp = ?,create_date = ?,modified_date = ? WHERE id = ?";
		try {
			int columnSize = 12;
			int size = mails.size();
			Object[][] params = new Object[size][columnSize + 1];
			for (int i = 0; i < size; i++) {
				params[i][0] =mails.get(i).getId();
				params[i][1] =mails.get(i).getSenderId();
				params[i][2] =mails.get(i).getPlayerId();
				params[i][3] =mails.get(i).getTextType();
				params[i][4] =mails.get(i).getTitle();
				params[i][5] =mails.get(i).getBody();
				params[i][6] =mails.get(i).getBodyParamString();
				params[i][7] =mails.get(i).getItemsByte();
				params[i][8] =mails.get(i).getStatus();
				params[i][9] =mails.get(i).getDsp();
				params[i][10] =mails.get(i).getCreateDate();
				params[i][11] =mails.get(i).getModifiedDate();
				params[i][columnSize] =mails.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0 ? is : new int[]{};
		} catch (Exception e) {
			log.error(e);
			return new int[]{};
		} finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
	}
	
	public int[] updateBatch(List<Mail> mails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return updateBatch(mails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean delete(Mail mail){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(mail,conn);
	}
	
	public boolean delete(Mail mail,Connection conn){
		return delete(mail,conn,TABLE_NAME);
	}
	
	public boolean delete(Mail mail,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return delete(mail,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean delete(Mail mail,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return delete(mail,conn,tableName);
	}
	
	public boolean delete(Mail mail,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int i = run.update(conn,sql, mail.getId());
			return i > 0 ? true : false;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean delete(Mail mail,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return delete(mail,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public boolean deleteBatch(List<Mail> mails){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(mails,conn);
	}
	
	public boolean deleteBatch(List<Mail> mails,Connection conn){
		return deleteBatch(mails,conn,TABLE_NAME);
	}
	
	public boolean deleteBatch(List<Mail> mails,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(mails,conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean deleteBatch(List<Mail> mails,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return deleteBatch(mails,conn,tableName);
	}
	
	public boolean deleteBatch(List<Mail> mails,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DELETE FROM " + tableName + " WHERE id = ?";
		try {
			int size = mails.size();
			Object[][] params = new Object[size][1];
			for (int i = 0; i < size; i++) {
				params[i][0] = mails.get(i).getId();
			}
			int[] is = run.batch(conn,sql,params);
			return is.length > 0;
		} catch (Exception e) {
			log.error(e);
			return false;
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean deleteBatch(List<Mail> mails,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return deleteBatch(mails,conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}

	/**
	 * 根据( id ) 查询
	 */
	public Mail getById(long id){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn);
	}
	
	public Mail getById(long id,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getById(id, conn,tableName);
	}
	public List<Mail> getIn(Set<Long> ids){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn);
	}
	
	public List<Mail> getIn(Set<Long> ids,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getIn(ids, conn,tableName);
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<Mail> getByPlayerId(int playerId){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerId(playerId, conn);
	}
	
	public List<Mail> getByPlayerId(int playerId,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerId(playerId, conn,tableName);
	}
	
	public List<Mail> getByPagePlayerId(int playerId,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerId(playerId, conn,page,pageSize);
	}
	
	public List<Mail> getByPagePlayerId(int playerId,String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerId(playerId, conn,tableName,page,pageSize);
	}
	/**
	 * 根据( player_id  status ) 查询
	 */
	public List<Mail> getByPlayerIdStatus(int playerId, int status){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerIdStatus(playerId,status, conn);
	}
	
	public List<Mail> getByPlayerIdStatus(int playerId, int status,String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPlayerIdStatus(playerId,status, conn,tableName);
	}
	
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerIdStatus(playerId,status, conn,page,pageSize);
	}
	
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getByPagePlayerIdStatus(playerId,status, conn,tableName,page,pageSize);
	}
	
	//Connection
	/**
	 * 根据( id ) 查询
	 */
	public Mail getById(long id,Connection conn){
		return getById(id,conn,TABLE_NAME);
	}
	
	public Mail getById(long id,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "id = ? ORDER BY id ASC LIMIT 1";
		Mail mail = null; 
		try {
			mail = run.query(conn,sql, new MailHandler(), id);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mail;
	}
	public List<Mail> getIn(Set<Long> ids,Connection conn){
		return getIn(ids,conn,TABLE_NAME);
	}
	
	public List<Mail> getIn(Set<Long> ids,Connection conn,String tableName){
		List<Mail> mails = Lists.newArrayList();
		if(ids.isEmpty()) return mails;
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "id in (%s) ORDER BY id ASC";
		sql = String.format(sql, Joiner.on(",").join(ids));
		try {
			mails = run.query(conn,sql, new MailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<Mail> getByPlayerId(int playerId,Connection conn){
		return getByPlayerId(playerId,conn,TABLE_NAME);
	}
	
	public List<Mail> getByPlayerId(int playerId,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "player_id = ? ORDER BY id ASC";
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler(), playerId);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	
	//-----------------------------------page-----------------------------------
	public List<Mail> getByPagePlayerId(int playerId,Connection conn,int page,int pageSize){
		return getByPagePlayerId(playerId,conn,TABLE_NAME,page,pageSize);
	}
	
	public List<Mail> getByPagePlayerId(int playerId,Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "player_id = ? ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler(), playerId);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	/**
	 * 根据( player_id  status ) 查询
	 */
	public List<Mail> getByPlayerIdStatus(int playerId, int status,Connection conn){
		return getByPlayerIdStatus(playerId,status,conn,TABLE_NAME);
	}
	
	public List<Mail> getByPlayerIdStatus(int playerId, int status,Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "player_id = ? AND status = ? ORDER BY id ASC";
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler(), playerId,status);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	
	//-----------------------------------page-----------------------------------
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,Connection conn,int page,int pageSize){
		return getByPagePlayerIdStatus(playerId,status,conn,TABLE_NAME,page,pageSize);
	}
	
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " WHERE " + "player_id = ? AND status = ? ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler(), playerId,status);
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	
	//DataSource
	/**
	 * 根据( id ) 查询
	 */
	public Mail getById(long id,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public Mail getById(long id,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getById(id, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	public List<Mail> getIn(Set<Long> ids,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getIn(Set<Long> ids,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getIn(ids, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( player_id ) 查询
	 */
	public List<Mail> getByPlayerId(int playerId,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerId(playerId, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getByPlayerId(int playerId,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerId(playerId, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	//-----------------------------------page-----------------------------------
	public List<Mail> getByPagePlayerId(int playerId,DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerId(playerId, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getByPagePlayerId(int playerId,DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerId(playerId, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	/**
	 * 根据( player_id  status ) 查询
	 */
	public List<Mail> getByPlayerIdStatus(int playerId, int status,DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerIdStatus(playerId,status, conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getByPlayerIdStatus(int playerId, int status,DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getByPlayerIdStatus(playerId,status, conn, tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	//-----------------------------------page-----------------------------------
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerIdStatus(playerId,status, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getByPagePlayerIdStatus(int playerId, int status,DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getByPagePlayerIdStatus(playerId,status, conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	
	public List<Mail> getAll(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn);
	}
	
	public List<Mail> getAll(Connection conn){
		return getAll(conn,TABLE_NAME);
	}
	
	public List<Mail> getAll(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getAll(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAll(conn,tableName);
	}
	
	public List<Mail> getAll(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " ORDER BY id ASC";
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	
	public List<Mail> getAll(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return getAll(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public List<Mail> getAllPage(int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,page,pageSize);
	}
	
	public List<Mail> getAllPage(Connection conn,int page,int pageSize){
		return getAllPage(conn,TABLE_NAME,page,pageSize);
	}
	
	public List<Mail> getAllPage(DataSource ds,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Mail> getAllPage(String tableName,int page,int pageSize){
		Connection conn = DataSourceManager.me().getMainConnection();
		return getAllPage(conn,tableName,page,pageSize);
	}
	
	public List<Mail> getAllPage(Connection conn,String tableName,int page,int pageSize){
		QueryRunner run = new QueryRunner();
		page = ((page-1) * pageSize);
		String sql = "SELECT id,sender_id,player_id,text_type,title,body,body_param,items,status,dsp,create_date,modified_date FROM " + tableName + " ORDER BY id ASC LIMIT " + page + " , " +pageSize;
		List<Mail> mails = Lists.newArrayList();
		try {
			mails = run.query(conn, sql, new MailListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return mails;
	}
	
	public List<Mail> getAllPage(DataSource ds,String tableName,int page,int pageSize){
		try {
			Connection conn = ds.getConnection();
			return getAllPage(conn,tableName,page,pageSize);
		} catch (Exception e) {
			log.error(e);
			return null;
		}		
	}
	
	public boolean truncate(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn);
	}
	
	public boolean truncate(Connection conn){
		return truncate(conn,TABLE_NAME);
	}
	
	public boolean truncate(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean truncate(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return truncate(conn,tableName);
	}
	
	public boolean truncate(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "TRUNCATE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean truncate(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return truncate(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	// 执行自定义查询sql
	public List<Map<String,Object>> executeQuerySql(String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String sql){
		return executeQuerySql(conn,TABLE_NAME,sql);
	}
	
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String sql){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,sql);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	
	public List<Map<String,Object>> executeQuerySql(String tableName,String sql){
		Connection conn = DataSourceManager.me().getMainConnection();
		return executeQuerySql(conn,tableName);
	}
	
	public List<Map<String,Object>> executeQuerySql(Connection conn,String tableName,String sql){
		QueryRunner run = new QueryRunner();
		ToolError.isAndTrue(ArtifactErrorCode.SQL_PERMISSIONS_ERROR, !StrUtil.startWithIgnoreCase(sql, "SELECT"));
		List<Map<String,Object>> list = Lists.newArrayList();
		try {
			list = run.query(conn,sql, new MapListHandler());
		} catch (Exception e) {
			log.error(e);
		}finally {
			try{
				DbUtils.close(conn);
			}catch (Exception e1) {
				log.error(e1);
				return null;
			}
		}
		return list;
	}
	/**
	public List<Map<String,Object>> executeQuerySql(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return executeQuerySql(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return null;
		}
	}
	*/
	
	//
	
	//
	
	//Drop Table
	public boolean drop(){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn);
	}
	
	public boolean drop(Connection conn){
		return drop(conn,TABLE_NAME);
	}
	
	public boolean drop(DataSource ds){
		try {
			Connection conn = ds.getConnection();
			return drop(conn);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean drop(String tableName){
		Connection conn = DataSourceManager.me().getMainConnection();
		return drop(conn,tableName);
	}
	
	public boolean drop(Connection conn,String tableName){
		QueryRunner run = new QueryRunner();
		String sql = "DROP TABLE " + tableName;
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean drop(DataSource ds,String tableName){
		try {
			Connection conn = ds.getConnection();
			return drop(conn,tableName);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	//create
	public boolean createTable(CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,type);
	}
	
	public boolean createTable(Connection conn,CreateTableEnum type){
		return createTable(conn,TABLE_NAME,type);
	}
	
	public boolean createTable(DataSource ds,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	public boolean createTable(String tableName,CreateTableEnum type){
		Connection conn = DataSourceManager.me().getMainConnection();
		return createTable(conn,tableName,type);
	}
	
	public boolean createTable(Connection conn,String tableName,CreateTableEnum type){
		QueryRunner run = new QueryRunner();
		SK_Plus plus = SK_Plus.b("CREATE TABLE IF NOT EXISTS `", tableName,"` (");
		plus.a("  `id` BIGINT(20) NOT NULL  COMMENT '',");	
		plus.a("  `sender_id` INT(11)   COMMENT 'remarks=发送者',");	
		plus.a("  `player_id` INT(11) NOT NULL  COMMENT 'remarks=收件人id',");	
		plus.a("  `text_type` INT(11)   COMMENT 'remarks=文字类型',");	
		plus.a("  `title` VARCHAR(100)   COMMENT 'remarks=邮件标题',");	
		plus.a("  `body` VARCHAR(1000)   COMMENT 'remarks=邮件正文',");	
		plus.a("  `body_param` VARCHAR(200)   COMMENT 'remarks=内容参数',");	
		plus.a("  `items` BLOB(65535)   COMMENT 'remarks=附件',");	
		plus.a("  `status` SMALLINT(4)   COMMENT 'remarks=状态：0未读,1已读,2已收,3已删',");	
		plus.a("  `dsp` VARCHAR(200)   COMMENT 'remarks=附件来源',");	
		plus.a("  `create_date` DATETIME NOT NULL  COMMENT '',");
		plus.a("  `modified_date` DATETIME NOT NULL  COMMENT '',");
		plus.a("  PRIMARY KEY (`id`),");
		if (CreateTableEnum.LOGIC.equals(type) || CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  KEY `fk_mail_player1_idx` (`player_id`),");
			plus.a("  KEY `playerId_status` (`player_id`,`status`),");
			
		}
		if (CreateTableEnum.DESIGN.equals(type)) {
			plus.a("  CONSTRAINT `fk_mail_player1` FOREIGN KEY (`player_id`) REFERENCES `player` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,");
		}
		String sql = plus.e();
		sql = StrUtil.subPre(sql, sql.lastIndexOf(StrUtil.C_COMMA));
		sql+=") ENGINE=InnoDB DEFAULT CHARSET=utf8  COMMENT='remarks=邮件';";
		try {
			run.update(conn, sql);
			return true;
		} catch (Exception e) {
			log.error(e);
			return false;
		} finally {
			try {
				DbUtils.close(conn);
			} catch (Exception e1) {
				log.error(e1);
				return false;
			}
		}
	}
	
	public boolean createTable(DataSource ds,String tableName,CreateTableEnum type){
		try {
			Connection conn = ds.getConnection();
			return createTable(conn,tableName,type);
		} catch (Exception e) {
			log.error(e);
			return false;
		}
	}
	
	
	public String getTableName(){
		return TABLE_NAME;
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getColumnsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"id\",\"NULLABLE\":0,\"REMARKS\":\"\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":1,\"DATA_TYPE\":-5,\"TYPE_NAME\":\"BIGINT\",\"COLUMN_SIZE\":20}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"sender_id\",\"NULLABLE\":1,\"REMARKS\":\"remarks=发送者\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":2,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"player_id\",\"NULLABLE\":0,\"REMARKS\":\"remarks=收件人id\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":3,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"text_type\",\"NULLABLE\":1,\"REMARKS\":\"remarks=文字类型\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":4,\"DATA_TYPE\":4,\"TYPE_NAME\":\"INT\",\"COLUMN_SIZE\":11}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"title\",\"NULLABLE\":1,\"REMARKS\":\"remarks=邮件标题\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":300,\"ORDINAL_POSITION\":5,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":100}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"body\",\"NULLABLE\":1,\"REMARKS\":\"remarks=邮件正文\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":3000,\"ORDINAL_POSITION\":6,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":1000}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"body_param\",\"NULLABLE\":1,\"REMARKS\":\"remarks=内容参数\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":600,\"ORDINAL_POSITION\":7,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":200}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"items\",\"NULLABLE\":1,\"REMARKS\":\"remarks=附件\ntype=Map\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":65535,\"ORDINAL_POSITION\":8,\"DATA_TYPE\":-4,\"TYPE_NAME\":\"BLOB\",\"COLUMN_SIZE\":65535}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"status\",\"NULLABLE\":1,\"REMARKS\":\"remarks=状态：0未读,1已读,2已收,3已删\",\"DECIMAL_DIGITS\":0,\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":9,\"DATA_TYPE\":5,\"TYPE_NAME\":\"SMALLINT\",\"COLUMN_SIZE\":4}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"YES\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"dsp\",\"NULLABLE\":1,\"REMARKS\":\"remarks=附件来源\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"CHAR_OCTET_LENGTH\":600,\"ORDINAL_POSITION\":10,\"DATA_TYPE\":12,\"TYPE_NAME\":\"VARCHAR\",\"COLUMN_SIZE\":200}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"create_date\",\"NULLABLE\":0,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":11,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
       	sp.a("{\"TABLE_CAT\":\"dal_game_design\",\"BUFFER_LENGTH\":65535,\"IS_NULLABLE\":\"NO\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"modified_date\",\"NULLABLE\":0,\"REMARKS\":\"\",\"NUM_PREC_RADIX\":10,\"SQL_DATETIME_SUB\":0,\"IS_GENERATEDCOLUMN\":\"NO\",\"IS_AUTOINCREMENT\":\"NO\",\"SQL_DATA_TYPE\":0,\"ORDINAL_POSITION\":12,\"DATA_TYPE\":93,\"TYPE_NAME\":\"DATETIME\",\"COLUMN_SIZE\":19}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
	
	@SuppressWarnings("unchecked")
	public List<Map<String,Object>> getIndexsInfo(){
		SK_Plus sp = SK_Plus.newSK_Plus();
		sp.a("[");
       	sp.a("{\"NON_UNIQUE\":false,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"id\",\"INDEX_NAME\":\"PRIMARY\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":true,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"player_id\",\"INDEX_NAME\":\"fk_mail_player1_idx\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":true,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"player_id\",\"INDEX_NAME\":\"playerId_status\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":1,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
       	sp.a("{\"NON_UNIQUE\":true,\"TABLE_CAT\":\"dal_game_design\",\"TABLE_NAME\":\"mail\",\"COLUMN_NAME\":\"status\",\"INDEX_NAME\":\"playerId_status\",\"ASC_OR_DESC\":\"A\",\"INDEX_QUALIFIER\":\"dal_game_design\",\"ORDINAL_POSITION\":2,\"CARDINALITY\":0,\"TYPE\":3}").a(",");
        sp.a("]");
        return JsonManager.me().getJson().parse(sp.e(), List.class);
	}
//自定义内容起始位置
class MailHandler implements ResultSetHandler<Mail>{
	@Override
	public Mail handle(ResultSet rs) throws SQLException {
		return rs.next() ? Mail.builder().createForResultSet(rs).syncIndexValues() : null ;
	}
}

class MailListHandler extends AbstractListHandler<Mail>{
	@Override
	protected Mail handleRow(ResultSet rs) throws SQLException {
		return Mail.builder().createForResultSet(rs).syncIndexValues();
	}
}

//自定义内容结束位置
}

